<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1、导入mapbox的依赖 -->
    <script src='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }
    </style>
</head>

<body>
    <!-- 2、设置地图容器的挂载点 -->
    <div id="map">

    </div>
    <script>
        /* token */
        mapboxgl.accessToken = 'pk.eyJ1IjoiZXJpYzA5MTciLCJhIjoiY2x3cTI2Zzg0MWY2dDJqcWVpdDI1MHNpcCJ9.k9gLW7ZgLyISNdYHIEEGdQ'
        const map = new mapboxgl.Map({
            /* target-ol */
            container: 'map', // container ID
            /* style-layers */
            style: 'mapbox://styles/mapbox/streets-v12', // style URL
            center: [114.30, 30.50], // starting position [lng, lat]
            zoom: 1, // starting zoom
        });
        var data = {
            type: 'FeatureCollection',
            features: [
                {
                    type: "Feature",
                    geometry: {
                        type: "Point",
                        coordinates: [114.30, 30.50]
                    },
                    properties: {
                        name: "武昌"
                    }
                }, {
                    type: "Feature",
                    geometry: {
                        type: "Point",
                        coordinates: [120, 30.50]
                    },
                    properties: {
                        name: "上海"
                    }
                }
            ]
        }
        map.on("style.load", () => {
            map.addSource("point-source", {
                type: "geojson",
                data
            })
            map.addLayer({
                id: "point-layer",
                source: "point-source",
                type: "circle",
                paint: {
                    'circle-radius': 20,
                    'circle-color': '#ff2d51',
                    'circle-opacity': 0.5,
                    'circle-stroke-width': 4,
                    'circle-stroke-color': "#fff"
                }
            })
            /* 设置鼠标的形状 */
            map.on("mouseenter", 'point-layer', () => {
                map.getCanvas().style.cursor = "wait"
            })
            map.on("mouseleave", 'point-layer', () => {
                map.getCanvas().style.cursor = "default"
            })
        })
        map.on('click', 'point-layer', evt => {
            console.log(evt.features[0])
            var { name } = evt.features[0].properties;
            alert(name)
        })
    </script>
</body>

</html>